/**
 * Definition for isBadVersion()
 * 
 * @param {integer} version number
 * @return {boolean} whether the version is bad
 */
 const bad = 4;
const isBadVersion = function (version) {
    if (version >= bad) {
      return true;
    }
    return false;
  };


/**
 * @param {function} isBadVersion()
 * @return {function}
 */
var solution = function (isBadVersion) {
  /**
   * @param {integer} n Total versions
   * @return {integer} The first bad version
   */
  return function (n) {
    let right = n;
    let left = 1;
    debugger
    while (left <= right) {
      const mid = Math.floor((left + right) / 2);
      // 如果true 版本错误 向左移动
      if (isBadVersion(mid)) {
        right = mid - 1;
      } else {
        // 如果false 版本正确 向右查找
        left = mid + 1;
      }
    }
    return left;
  };
};

console.log(solution(isBadVersion)(5));